﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ShanNiang.Hosting.Extensions
{
    /// <summary>
    /// 枚举扩展方法
    /// </summary>
    public static class EnumExtension
    {
        /// <summary>
        /// 获取枚举值
        /// </summary>
        /// <param name="en">当前枚举</param>
        /// <returns></returns>
        public static int GetValue(this Enum en)
        {
            return Convert.ToInt32(en);
        }

        /// <summary>
        /// 获取Description特性值
        /// </summary>
        /// <param name="en">当前枚举</param>
        /// <returns></returns>
        public static string? GetDescription(this Enum en)
        {
            var type = en.GetType();
            var field = type.GetField(en.ToString());
            if (field != null)
            {
                var attrs = field.GetCustomAttributes(typeof(DescriptionAttribute), false);
                if (attrs.Length > 0)
                {
                    DescriptionAttribute descriptionAttr = (DescriptionAttribute)attrs[0];
                    return descriptionAttr.Description;
                }
            }

            return null;
        }

        ///// <summary>
        ///// 获取Description特性值
        ///// </summary>
        ///// <param name="en">当前枚举</param>
        ///// <returns></returns>
        //public static string GetEnumName(this Enum en)
        //{
        //    var type = en.GetType();
        //    var filed = type.GetField(en.ToString());
        //    if (filed != null)
        //    {
        //        var attrs = filed.GetCustomAttributes(typeof(EnumNameAttribute), false);
        //        if (attrs.Length > 0)
        //        {
        //            EnumNameAttribute nameAttr = (EnumNameAttribute)attrs[0];
        //            return nameAttr.Name;
        //        }
        //    }

        //    return null;
        //}


    }
}
